﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Coolite.Ext.Web;
using GUESTSMANAGEMENT.Controller;

namespace GUESTSMANAGEMENT.View.ListsGuests
{
    public partial class frmListsGuests : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Carrega os dados na grid quando abre a tela.
            if (!Ext.IsAjaxRequest)
            {
                this.guestsStore.DataSource = Controller.Guests.getGridList();
                this.guestsStore.DataBind();
            }
        }

        //Reload na grid quando salva ou atualiza os dados.
        public Store GridStore
        {
            get;
            set;
        }

        //Atualiza grid quando á dado refesh.
        protected void Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            this.guestsStore.DataSource = Controller.Guests.getGridList();
            this.guestsStore.DataBind();
        }

        protected void SaveListsGuest(object sender, AjaxEventArgs e)
        {

            try
            {
                gmevent evento = Controller.Event.getByEvent(int.Parse(this.txt_idCdEvent.Text));

                string teste = evento.DTEVENT.ToString().Replace("00:00:00", evento.TMLIST.ToString());

                DateTime date = Convert.ToDateTime(teste);
                if (!((DateTime.Compare(DateTime.Now, date)) >= 0))
                {

                    string id = this.txt_idGuest.Text;

                    int cdUser = Convert.ToInt32(Session["userCd"]);
                    gmuser user = Controller.User.getByUser((int)cdUser);
                    gmguest guest = Controller.Guests.getByGuests(int.Parse(id));
                    gmlistguests listsGuestVerify = Controller.GuestsList.getByListsGuestMail(guest.NMMAIL, (int)evento.CDEVENT);

                    if (listsGuestVerify == null)
                    {
                        Session["guestId"] = (int)guest.CDGUEST;
                        Session["eventId"] = (int)evento.CDEVENT;
                        Session["guestName"] = guest.NMGUEST.ToString();
                        Session["guestMail"] = guest.NMMAIL.ToString();

                        this.ScriptManagerProxy.AddScript("newGuestInvite();");
                        this.GridStore.AddScript(string.Format("{0}.reload();", this.GridStore.ClientID));
                    }
                    else
                    {
                        Ext.Msg.Alert("Alerta!", "Convidado já cadastrado para este evento pelo Promoter <b>" + user.NMUSER + "</b>").Show();
                    }
                }
                else
                {
                    Ext.Msg.Alert("Alerta!", "Tempo limite para inclusão de convidados esgotou!").Show();
                }
            }
            catch
            {
                
            }
            /*try
            {
                string id = this.txt_idGuest.Text;

                int cdUser = Convert.ToInt32(Session["userCd"]);
                using (guestsmanagementEntities context = new guestsmanagementEntities())
                {
                    gmguest guest = Controller.Guests.getByGuests(int.Parse(id));
                    gmuser user = Controller.User.getByUser(cdUser);
                    gmevent evento = Controller.Event.getByEvent(int.Parse(this.txt_idCdEvent.Text));
                    gmlistguests listsGuestVerify = Controller.GuestsList.getByListsGuestMail(guest.NMMAIL, (int)evento.CDEVENT);

                    if (listsGuestVerify == null)
                    {
                        gmlistguests listsGuests = new gmlistguests();
                        listsGuests.GMEVENT_CDEVENT = evento.CDEVENT;
                        listsGuests.GMGUEST_CDGUEST = guest.CDGUEST;
                        listsGuests.GMUSER_CDUSER = user.CDUSER;
                        listsGuests.NMGUEST = guest.NMGUEST;
                        listsGuests.NMMAILGUEST = guest.NMMAIL;
                        Controller.GuestsList.addListsGuests(listsGuests);
                        //ClearWindow(Controls);
                        this.GridStore.AddScript(string.Format("{0}.reload();", this.GridStore.ClientID));
                        FormStatusBar.SetStatus(new StatusBarStatusConfig { Text = "Convidado Cadastro com sucesso!", IconCls = "", Clear2 = true });
                    }
                    else
                    {
                        Ext.Msg.Alert("Alerta!", "Convidado já cadastrado para este evento!").Show();
                    }
                }
            }
            catch
            {
            }*/
        }


        protected void Close(object sender, AjaxEventArgs e)
        {
            this.winGrdListsGuests.Hide();
            ClearWindow(Controls);
        }

        //Método para excluir.
        protected void ConfirmDeleteGuests(object sender, AjaxEventArgs e)
        {
            string selectGuests = this.txt_idGuest.Text.Trim();
            gmguest guests = Controller.Guests.getByGuests(int.Parse(selectGuests));
            Controller.Guests.deletGuests(guests);
            this.GridStore.AddScript(string.Format("{0}.reload();", this.GridStore.ClientID));
            Ext.Msg.Alert("Alerta!", "Convidado <b>" + guests.NMGUEST + "</b> excluído com sucesso!").Show();

        }

        public void SetEvent(int id)
        {
            this.txt_idCdEvent.Text = id.ToString();
        }

        public void ClearWindow(ControlCollection AllControls)
        {
            foreach (Control c in AllControls)
            {
                if (c is TextField)
                {
                    ((TextField)c).Text = "";
                }
                if (c is ComboBox)
                {
                    ((ComboBox)c).SetValue("");
                }
                if (c is Checkbox)
                {
                    ((Checkbox)c).Checked = true;
                }
                if (c is DateField)
                {
                    ((DateField)c).SetValue("");
                }
                if (c is TextArea)
                {
                    ((TextArea)c).Text = "";
                    ((TextArea)c).Visible = false;
                }
                if (c is Hidden)
                {
                    ((Hidden)c).Text = "";
                }
                this.winGrdListsGuests.Restore();
                ClearWindow(c.Controls);
            }
        }
    }
}